package leetcode;

import java.util.HashMap;
import java.util.Map;

public class LengthOfLongestSubstring {
    public static void main(String[] args) {
        LengthOfLongestSubstring lengthOfLongestSubstring = new LengthOfLongestSubstring();
        System.out.println(lengthOfLongestSubstring.lengthOfLongestSubstring("pwwkew"));
    }

    public int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        Map<Character, Integer> map = new HashMap<>();
        int ans = 0;
        int i = 0;
        for (int j = 0; j < chars.length; j++) {
            if (map.containsKey(chars[j])) {
                i = Math.max(i, map.get(chars[j]) + 1);
            }
            map.put(chars[j], j);
            ans = Math.max(j - i + 1, ans);
        }
        return ans;
    }
}
